# -*- coding: utf-8 -*-
import scrapy
import sys
sys.path.append('..')
from movie.items import MovieItem

class ImdbSpider(scrapy.Spider):
    name = 'imdb'
    allowed_domains = ['www.imdb.com']
    start_urls = ['https://www.imdb.com/chart/top']

    def parse(self, response):
        movies = response.xpath('//tbody[@class="lister-list"]/tr')
        for subMovie in movies:
            item = MovieItem()
            item['name'] = subMovie.xpath('./td[@class="titleColumn"]/a/text()').extract()[0]
            item['url']  = 'https://www.imdb.com' + subMovie.xpath('././td[@class="titleColumn"]/a/@href').extract()[0]
            yield scrapy.Request(url=item['url'], meta={'item' : item}, callback=self.parseSub, dont_filter=True)


    def parseSub(self, response):
        items = response.meta['item']
        #解析余下参数
        sumObj = response.xpath('//div[@class="credit_summary_item"]')
        director = sumObj[0].xpath('./a/text()').extract()[0]
        items['director'] = director
        items['writer'] = sumObj[1].xpath('./a/text()').extract()
        items['actor'] = sumObj[2].xpath('./a/text()').extract()
        yield items